package act.impl;

import dao.DAOFactory;
import dao.tro.Flight;
import frwa.ActionResult;
import frwa.IAction;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;

public class ShowFlightInformation implements IAction{
    private final static Logger log =Logger.getLogger(ShowFlightInformation.class);

    public String getName() {
        return "ShowFlightInformation";
    }
    public ActionResult perform(HttpServletRequest request) {
        DAOFactory factory = DAOFactory.getDAOFactory(1);
        Long trui=null;
        try {
            trui = Long.parseLong(request.getParameter("flightNumber"));
        } catch (NumberFormatException e) {
            log.info("I catch an exception "+e);
        }
        Flight flight = factory.getFlightDAO().showInformation(trui);
        flight.setDepartureAirport(factory.getAirportDAO().loadCountryName(flight.getDepartureAirport()));
        flight.setDestinationAirport(factory.getAirportDAO().loadCountryName(flight.getDestinationAirport()));
        ActionResult actionResult = new ActionResult();
        actionResult.setUrlAddress("flt/ShowFlightInformation.jsp");
        if (flight!=null) {
            actionResult.setUrlAddress("/flt/ShowFlightInformation.jsp");
        } else {
            actionResult.setUrlAddress("flt/FlightNotFound.jsp");
        }
        FlightFormObject flightFormObject = new FlightFormObject();
        flightFormObject.setFlight(flight);
        actionResult.setFormObject(flightFormObject);
        return actionResult;
    }
}
